package de.hauck.rtmidlet.views;

/*
 * Copyright (C) 2006 Leopardo.f
 *
 * This file is part of 2(o)(o)6 World Kups, a J2ME jigsaw puzzle.
 *
 * 2(o)(o)6 World Kups is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * 2(o)(o)6 World Kups is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with 2(o)(o)6 World Kups; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.  Or, visit http://www.gnu.org/copyleft/gpl.html
 */

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import de.hauck.rtmidlet.model.Configuration;

public class PopupMenu
{
	private int nStartX, nStartY, nViewableLines = 6, nLineHeight, nLineWidth, nCanvasWidth, nCanvasHeight, nMenuWidth, nMenuHeight;
	private byte yOuterBorder = 2, yColorBorder = 2, yInnerBorder = 2;
	private int nBackColor = Configuration.COLOR, nForeColor = 0x00000000; 

	private String[] saOptions;
	private Graphics g;
	private Font f;
	
	public PopupMenu (Graphics g, Font f, int nWidth, int nHeight, String[] sa)
	{
		saOptions = sa;

		this.g = g;
		this.f = f;
		this.nCanvasWidth = nWidth;
		this.nCanvasHeight = nHeight;
		nLineHeight = f.getHeight();
		if (nLineHeight < 0)
			nLineHeight = 20;

		nLineWidth = 0;
		for (int i = 0; i < saOptions.length; i++)
			if (f.stringWidth (saOptions[i]) > nLineWidth)
				nLineWidth = f.stringWidth (saOptions[i]);
                if (nLineWidth < nWidth - 30)
                    nLineWidth += 20;

//                System.out.println ("saOptions.length=" + saOptions.length + ", nViewableLines=" + nViewableLines);
		if (saOptions.length < nViewableLines)
			nViewableLines = saOptions.length;
//                System.out.println ("saOptions.length=" + saOptions.length + ", nViewableLines=" + nViewableLines);

                nMenuWidth = 2 * (yOuterBorder + yColorBorder + yInnerBorder) + nLineWidth;
		nStartX = (nCanvasWidth - nMenuWidth) / 2;
		nMenuHeight = 2 * (yOuterBorder + yColorBorder + yInnerBorder) + nViewableLines * nLineHeight;
		nStartY = (nCanvasHeight - nMenuHeight) / 2;
	}
		
	public void showMenu (byte yHighlightIndex, byte yStartIndex)
	{
		int i, nCurrInd, nCurrY, nC = g.getColor();
		Font fOld = g.getFont();

		if (yHighlightIndex < 0)
			yHighlightIndex = 0;
		else if (yHighlightIndex >= nViewableLines)
			yHighlightIndex = (byte) nViewableLines;


		g.setColor (nBackColor);
		g.fillRoundRect(nStartX, nStartY, nMenuWidth, nMenuHeight,8,8);
		g.setColor (nForeColor);
		for (i = 0; i < yColorBorder; i++)
			g.drawRoundRect (nStartX + yOuterBorder + i, nStartY + yOuterBorder + i, nMenuWidth - 2 * (yOuterBorder + i) - 1, nMenuHeight - 2 * (yOuterBorder + i) - 1,8,8);

		nCurrY = nStartY + yOuterBorder + yColorBorder + yInnerBorder;
		nCurrInd = yStartIndex;
                g.setFont (f);
		for (i = 0; i < yHighlightIndex; i++)
		{
			g.drawString (saOptions[nCurrInd % saOptions.length], nStartX + yOuterBorder + yColorBorder + yInnerBorder, nCurrY, Graphics.TOP | Graphics.LEFT);
			nCurrY += nLineHeight;
			nCurrInd++;
		}

		g.fillRect (nStartX + yOuterBorder + yColorBorder + yInnerBorder, nCurrY, nLineWidth, nLineHeight);
                g.setColor (nBackColor);
		g.drawString (saOptions[nCurrInd % saOptions.length], nStartX + yOuterBorder + yColorBorder + yInnerBorder, nCurrY+2, Graphics.TOP | Graphics.LEFT);
		nCurrY += nLineHeight;
		nCurrInd++;
                g.setColor (nForeColor);

		for (i = yHighlightIndex + 1; i < nViewableLines; i++)
		{
			g.drawString (saOptions[nCurrInd % saOptions.length], nStartX + yOuterBorder + yColorBorder + yInnerBorder, nCurrY+2, Graphics.TOP | Graphics.LEFT);
			nCurrY += nLineHeight;
			nCurrInd++;
		}

		g.setColor (nC);
                g.setFont (fOld);
	}

	public void showMenu()
	{
		showMenu ((byte) 0, (byte) 0);
	}
}
